# -*- coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait


class BaseFunctions():
    def __init__(self, url):
        self.timeout = 20
        self.url = url

    def getDriver(self,browser="Chrome", option=False):
        if browser=="Chrome":
            if option:
                options = self.__setOptions(browser)
                driver = webdriver.Chrome(options=options)
            else:
                driver = webdriver.Chrome()

            return driver

        elif browser == "Firefox":
            if option:
                options = self.__setOptions(browser)
                driver = webdriver.Chrome(options=options)
            else:
                driver = webdriver.Firefox()

            return driver

    def __setOptions(self,browser):
        if browser == "Chrome":
            options = webdriver.ChromeOptions()
            options.add_argument("--user-data-dir=" + r"C:/Users/Administrator/AppData/Local/Google/Chrome/User Data")
            return options
        elif browser == "Firefox":
            pass

    def openURL(self,driver):
        driver.get(self.url)
        driver.maximize_window()

    def findElements(self,driver, loc):
        WebDriverWait(driver, self.timeout).until(lambda x: x.find_element(loc[0], loc[1]))
        return driver.find_elements(loc[0], loc[1])

    def getText(self, element):
        return element.text

    def getAttr(self, element, name=None):
        return element.get_attribute(name)